using System;
using System.Data;
using System.Configuration;
using CodeWork.Library.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Data.SqlTypes;
using CodeWork.Library;
using System.Text;
using System.Data.SqlClient;

/// <summary>
/// ORM object that maps to tbl_siteuser
/// </summary>
namespace CodeWork.DAL.DataContainers.SqlServer
{
    public class Siteuser : SqlServerDAO
    {
        /// <summary>
        /// Self table parameters
        /// </summary>
        private int _id;
		private string _userId;
		private int _userroleId;
		private string _userPassword;
		private string _userEmail;
		private bool _isActive;
		private bool _isOnline;
		private bool _isConfirmed;
		private string _confirmationCode;
		private DateTime _registerDate;
		private DateTime _lastLogin;
		private string _securityQuestion;
		private string _securityAnswer;

        /// <summary>
        /// Foriegn key parameters
        /// </summary>
        private List<Userprofile> _siteuserUserprofile;
		private bool _isSiteuserUserprofileLoaded;
		

        /// <summary>
        /// Self table properties
        /// </summary>
		public int Id
		{
			get { return this._id; }
			set { this._id = value; }
		}

		public string UserId
		{
			get { return this._userId; }
			set { this._userId = value; }
		}

		public int UserroleId
		{
			get { return this._userroleId; }
			set { this._userroleId = value; }
		}

		public string UserPassword
		{
			get { return this._userPassword; }
			set { this._userPassword = value; }
		}

		public string UserEmail
		{
			get { return this._userEmail; }
			set { this._userEmail = value; }
		}

		public bool IsActive
		{
			get { return this._isActive; }
			set { this._isActive = value; }
		}

		public bool IsOnline
		{
			get { return this._isOnline; }
			set { this._isOnline = value; }
		}

		public bool IsConfirmed
		{
			get { return this._isConfirmed; }
			set { this._isConfirmed = value; }
		}

		public string ConfirmationCode
		{
			get { return this._confirmationCode; }
			set { this._confirmationCode = value; }
		}

		public DateTime RegisterDate
		{
			get { return this._registerDate; }
			set { this._registerDate = value; }
		}

		public DateTime LastLogin
		{
			get { return this._lastLogin; }
			set { this._lastLogin = value; }
		}

		public string SecurityQuestion
		{
			get { return this._securityQuestion; }
			set { this._securityQuestion = value; }
		}

		public string SecurityAnswer
		{
			get { return this._securityAnswer; }
			set { this._securityAnswer = value; }
		}
		
        /// <summary>
        /// Foreign key properties, loaded on demand only
        /// </summary>
        public List<Userprofile> SiteuserUserprofile
		{
			get
			{
				if (!this._isSiteuserUserprofileLoaded)
				{
					List<Condition> conditions = new List<Condition>();
					conditions.Add(new Condition("siteuser_id", ConditionOperator.Equal,this._id, JoinOperator.FirstValue));
					Userprofile userprofile = new Userprofile();
					this._siteuserUserprofile = userprofile.Load(conditions);
					this._isSiteuserUserprofileLoaded = true;
				}
				return this._siteuserUserprofile;
			}
			set
			{
				this._siteuserUserprofile = value;
				this._isSiteuserUserprofileLoaded = true;
			}
		}
		

        /// <summary>
        /// Empty Constructor
        /// </summary>
        public Siteuser() { }

        /// <summary>
        /// Parameterized constructor
        /// </summary>
        /// <param name="id"></param>
		/// <param name="userId"></param>
		/// <param name="userroleId"></param>
		/// <param name="userPassword"></param>
		/// <param name="userEmail"></param>
		/// <param name="isActive"></param>
		/// <param name="isOnline"></param>
		/// <param name="isConfirmed"></param>
		/// <param name="confirmationCode"></param>
		/// <param name="registerDate"></param>
		/// <param name="lastLogin"></param>
		/// <param name="securityQuestion"></param>
		/// <param name="securityAnswer"></param>
        public Siteuser
        (
            int id,
			string userId,
			int userroleId,
			string userPassword,
			string userEmail,
			bool isActive,
			bool isOnline,
			bool isConfirmed,
			string confirmationCode,
			DateTime registerDate,
			DateTime lastLogin,
			string securityQuestion,
			string securityAnswer
        )
        {
            this._id = id;
			this._userId = userId;
			this._userroleId = userroleId;
			this._userPassword = userPassword;
			this._userEmail = userEmail;
			this._isActive = isActive;
			this._isOnline = isOnline;
			this._isConfirmed = isConfirmed;
			this._confirmationCode = confirmationCode;
			this._registerDate = registerDate;
			this._lastLogin = lastLogin;
			this._securityQuestion = securityQuestion;
			this._securityAnswer = securityAnswer;
        }

        /// <summary>
        /// Static save method. Saves the current instance in the table using the provided values using the
        /// primary key id. Expects that the Id is a primary key of an existing record in table.
        /// </summary>
        /// <param name="id"></param>
		/// <param name="userId"></param>
		/// <param name="userroleId"></param>
		/// <param name="userPassword"></param>
		/// <param name="userEmail"></param>
		/// <param name="isActive"></param>
		/// <param name="isOnline"></param>
		/// <param name="isConfirmed"></param>
		/// <param name="confirmationCode"></param>
		/// <param name="registerDate"></param>
		/// <param name="lastLogin"></param>
		/// <param name="securityQuestion"></param>
		/// <param name="securityAnswer"></param>
        /// <returns>Boolean result as success or failure</returns>
        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public static bool Save
        (
            int id,
			string userId,
			int userroleId,
			string userPassword,
			string userEmail,
			bool isActive,
			bool isOnline,
			bool isConfirmed,
			string confirmationCode,
			DateTime registerDate,
			DateTime lastLogin,
			string securityQuestion,
			string securityAnswer
        )
        {
            Siteuser siteuser = new Siteuser();
            siteuser.Id = id;
			siteuser.UserId = userId;
			siteuser.UserroleId = userroleId;
			siteuser.UserPassword = userPassword;
			siteuser.UserEmail = userEmail;
			siteuser.IsActive = isActive;
			siteuser.IsOnline = isOnline;
			siteuser.IsConfirmed = isConfirmed;
			siteuser.ConfirmationCode = confirmationCode;
			siteuser.RegisterDate = registerDate;
			siteuser.LastLogin = lastLogin;
			siteuser.SecurityQuestion = securityQuestion;
			siteuser.SecurityAnswer = securityAnswer;

            return siteuser.Save();
        }

        /// <summary>
        /// Saves the current instance and returns result
        /// </summary>
        /// <returns>Boolean result as success or failure</returns>
        public override bool Save()
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddCommandParameter(command, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, Id);
			AddCommandParameter(command, "@UserId", SqlDbType.NVarChar, 50, ParameterDirection.Input, UserId);
			AddCommandParameter(command, "@UserroleId", SqlDbType.Int, 0, ParameterDirection.Input, UserroleId);
			AddCommandParameter(command, "@UserPassword", SqlDbType.NVarChar, 150, ParameterDirection.Input, UserPassword);
			AddCommandParameter(command, "@UserEmail", SqlDbType.NVarChar, 50, ParameterDirection.Input, UserEmail);
			AddCommandParameter(command, "@IsActive", SqlDbType.Bit, 0, ParameterDirection.Input, IsActive);
			AddCommandParameter(command, "@IsOnline", SqlDbType.Bit, 0, ParameterDirection.Input, IsOnline);
			AddCommandParameter(command, "@IsConfirmed", SqlDbType.Bit, 0, ParameterDirection.Input, IsConfirmed);
			AddCommandParameter(command, "@ConfirmationCode", SqlDbType.NVarChar, 150, ParameterDirection.Input, ConfirmationCode);
			AddCommandParameter(command, "@RegisterDate", SqlDbType.DateTime, 0, ParameterDirection.Input, RegisterDate);
			AddCommandParameter(command, "@LastLogin", SqlDbType.DateTime, 0, ParameterDirection.Input, LastLogin);
			AddCommandParameter(command, "@SecurityQuestion", SqlDbType.NVarChar, 50, ParameterDirection.Input, SecurityQuestion);
			AddCommandParameter(command, "@SecurityAnswer", SqlDbType.NVarChar, 50, ParameterDirection.Input, SecurityAnswer);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_Siteuser_Save.ToString());

            int returnValue = (int)GetOneData(command);
            return (returnValue == 0 ? false : true);
        }

        /// <summary>
        /// Saves the current instance based on provided conditions. This method assumes that
        /// conditions are provided in correct order and in proper format.
        /// </summary>
        /// <param name="conditions">Custom condition based on which to match data</param>
        /// <returns>Boolean result as success or failure</returns>
        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public bool Save(List<Condition> conditions)
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            PrepareConditionalParameters(command, conditions);

            AddCommandParameter(command, "@NewUserId", SqlDbType.NVarChar, 50, ParameterDirection.Input, UserId);
			AddCommandParameter(command, "@NewUserroleId", SqlDbType.Int, 0, ParameterDirection.Input, UserroleId);
			AddCommandParameter(command, "@NewUserPassword", SqlDbType.NVarChar, 150, ParameterDirection.Input, UserPassword);
			AddCommandParameter(command, "@NewUserEmail", SqlDbType.NVarChar, 50, ParameterDirection.Input, UserEmail);
			AddCommandParameter(command, "@NewIsActive", SqlDbType.Bit, 0, ParameterDirection.Input, IsActive);
			AddCommandParameter(command, "@NewIsOnline", SqlDbType.Bit, 0, ParameterDirection.Input, IsOnline);
			AddCommandParameter(command, "@NewIsConfirmed", SqlDbType.Bit, 0, ParameterDirection.Input, IsConfirmed);
			AddCommandParameter(command, "@NewConfirmationCode", SqlDbType.NVarChar, 150, ParameterDirection.Input, ConfirmationCode);
			AddCommandParameter(command, "@NewRegisterDate", SqlDbType.DateTime, 0, ParameterDirection.Input, RegisterDate);
			AddCommandParameter(command, "@NewLastLogin", SqlDbType.DateTime, 0, ParameterDirection.Input, LastLogin);
			AddCommandParameter(command, "@NewSecurityQuestion", SqlDbType.NVarChar, 50, ParameterDirection.Input, SecurityQuestion);
			AddCommandParameter(command, "@NewSecurityAnswer", SqlDbType.NVarChar, 50, ParameterDirection.Input, SecurityAnswer);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_Siteuser_SaveConditional.ToString());

            int returnValue = (int)GetOneData(command);
            return (returnValue == 0 ? false : true);
        }

        /// <summary>
        /// Static creational method. This method actually adds a new entry in the table with
        /// the provided information.
        /// </summary>
        /// <param name="userId"></param>
		/// <param name="userroleId"></param>
		/// <param name="userPassword"></param>
		/// <param name="userEmail"></param>
		/// <param name="isActive"></param>
		/// <param name="isOnline"></param>
		/// <param name="isConfirmed"></param>
		/// <param name="confirmationCode"></param>
		/// <param name="registerDate"></param>
		/// <param name="lastLogin"></param>
		/// <param name="securityQuestion"></param>
		/// <param name="securityAnswer"></param>
        /// <returns>Instance of the created object</returns>
        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public static Siteuser Create
        (
            string userId,
			int userroleId,
			string userPassword,
			string userEmail,
			bool isActive,
			bool isOnline,
			bool isConfirmed,
			string confirmationCode,
			DateTime registerDate,
			DateTime lastLogin,
			string securityQuestion,
			string securityAnswer
        )
        {
            Siteuser siteuser = new Siteuser();
            siteuser.UserId = userId;
			siteuser.UserroleId = userroleId;
			siteuser.UserPassword = userPassword;
			siteuser.UserEmail = userEmail;
			siteuser.IsActive = isActive;
			siteuser.IsOnline = isOnline;
			siteuser.IsConfirmed = isConfirmed;
			siteuser.ConfirmationCode = confirmationCode;
			siteuser.RegisterDate = registerDate;
			siteuser.LastLogin = lastLogin;
			siteuser.SecurityQuestion = securityQuestion;
			siteuser.SecurityAnswer = securityAnswer;

            return (siteuser.Create() ? siteuser : null);
        }

        /// <summary>
        /// Instance creational method. Inserts the current object as a new entry in table.
        /// </summary>
        /// <returns>Boolean result as success or failure</returns>
        public override bool Create()
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddCommandParameter(command, "@UserId", SqlDbType.NVarChar, 50, ParameterDirection.Input, UserId);
			AddCommandParameter(command, "@UserroleId", SqlDbType.Int, 0, ParameterDirection.Input, UserroleId);
			AddCommandParameter(command, "@UserPassword", SqlDbType.NVarChar, 150, ParameterDirection.Input, UserPassword);
			AddCommandParameter(command, "@UserEmail", SqlDbType.NVarChar, 50, ParameterDirection.Input, UserEmail);
			AddCommandParameter(command, "@IsActive", SqlDbType.Bit, 0, ParameterDirection.Input, IsActive);
			AddCommandParameter(command, "@IsOnline", SqlDbType.Bit, 0, ParameterDirection.Input, IsOnline);
			AddCommandParameter(command, "@IsConfirmed", SqlDbType.Bit, 0, ParameterDirection.Input, IsConfirmed);
			AddCommandParameter(command, "@ConfirmationCode", SqlDbType.NVarChar, 150, ParameterDirection.Input, ConfirmationCode);
			AddCommandParameter(command, "@RegisterDate", SqlDbType.DateTime, 0, ParameterDirection.Input, RegisterDate);
			AddCommandParameter(command, "@LastLogin", SqlDbType.DateTime, 0, ParameterDirection.Input, LastLogin);
			AddCommandParameter(command, "@SecurityQuestion", SqlDbType.NVarChar, 50, ParameterDirection.Input, SecurityQuestion);
			AddCommandParameter(command, "@SecurityAnswer", SqlDbType.NVarChar, 50, ParameterDirection.Input, SecurityAnswer);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_Siteuser_Create.ToString());

            Id = (int)GetOneData(command);
            return (Id == 0 ? false : true);
        }

        /// <summary>
        /// Static deletion method. Deletes a record from table using the provided primary key id.
        /// </summary>
        /// <param name="id">Primary key</param>
        /// <returns>Boolean result as success or failure</returns>
        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Delete, true)]
        public static bool Delete(int id)
        {
            Siteuser siteuser = new Siteuser();
            siteuser.Id = id;

            return siteuser.Delete();
        }

        /// <summary>
        /// Static deletion method. Deletes a list record from table using the provided primary keys id.
        /// </summary>
        /// <param name="ids">List of primarky key id</param>
        /// <returns>Boolean result as success or failure</returns>
        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Delete, true)]
        public static bool Delete(List<int> ids)
        {
            string csvIds = Utility.MakeCSV(ids);

            Siteuser siteuser = new Siteuser();

            return siteuser.Delete(csvIds);
        }

        /// <summary>
        /// Static deletion method. Deletes records from table using provided conditions.
        /// </summary>
        /// <param name="conditions">Custom condition based on which to match data</param>
        /// <returns>Boolean result as success or failure</returns>
        public static bool Delete(List<Condition> conditions)
        {
            Siteuser siteuser = new Siteuser();

            return siteuser.ConditionalDelete(conditions);
        }

        /// <summary>
        /// This is helper method for conditional delete method above. Not for other general use.
        /// </summary>
        /// <param name="conditions">Custom condition based on which to match data</param>
        /// <returns>Boolean result as success or failure</returns>
        private bool ConditionalDelete(List<Condition> conditions)
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            PrepareConditionalParameters(command, conditions);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_Siteuser_DeleteConditional.ToString());

            int returnValue = (int)GetOneData(command);
            return (returnValue == 0 ? false : true);
        }

        /// <summary>
        /// This is a helper method for multiple id delete method. Not for general use.
        /// </summary>
        /// <param name="csvIds">Comma seperated primary key values as string</param>
        /// <returns>Boolean result as success or failure</returns>
        private bool Delete(string csvIds)
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddCommandParameter(command, "@Ids", SqlDbType.NVarChar, 500, ParameterDirection.Input, csvIds);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_Siteuser_DeleteMultiple.ToString());

            int returnValue = (int)GetOneData(command);
            return (returnValue == 0 ? false : true);
        }

        /// <summary>
        /// Instance deletion method. Deletes the current instance from table.
        /// </summary>
        /// <returns>Boolean result as success or failure</returns>
        public override bool Delete()
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddCommandParameter(command, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, Id);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_Siteuser_DeleteOne.ToString());

            int returnValue = (int)GetOneData(command);
            return (returnValue == 0 ? false : true);
        }

        /// <summary>
        /// Static selection method. Used for loading a perticular data using primary key id.
        /// </summary>
        /// <param name="id">Primary key</param>
        /// <returns>Instance of the loaded object</returns>
        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static Siteuser Load(int id)
        {
            Siteuser siteuser = new Siteuser();
            siteuser.Id = id;

            if (siteuser.Load())
                return siteuser;
            else
                return null;
        }

        /// <summary>
        /// Instance selection method that fetches data using paging and sorting and also with 
        /// custom conditions.
        /// </summary>
        /// <param name="oderBy">Sorting order</param>
        /// <param name="startRow">Page start position</param>
        /// <param name="pageSize">Number of maximum record to be shown</param>
        /// <param name="conditions">Custom condition based on which to match data</param>
        /// <returns>List of instances of loaded objects</returns>
        public List<Siteuser> Load(string orderBy, int startRow, int pageSize, List<Condition> conditions)
        {
            List<Siteuser> siteusers = new List<Siteuser>();
			
			SqlCommand command = (SqlCommand)CreateCommand();
			
            PrepareConditionalParameters(command, conditions);

            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddCommandParameter(command, "@OrderBy", SqlDbType.NVarChar, 50, ParameterDirection.Input, orderBy);
            AddCommandParameter(command, "@StartRow", SqlDbType.Int, 0, ParameterDirection.Input, startRow);
            AddCommandParameter(command, "@PageSize", SqlDbType.Int, 0, ParameterDirection.Input, pageSize);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_Siteuser_LoadConditionalPaged.ToString());

            List<BaseDAO> dataList = GetListData(command);

            foreach (BaseDAO data in dataList)
            {
                Siteuser siteuser = (Siteuser)data;
                siteusers.Add(siteuser);
            }

            return siteusers;
        }

        /// <summary>
        /// Counts the number of total record available
        /// </summary>
        /// <param name="conditions">Custom condition based on which to match data</param>
        /// <returns>Count of the records</returns>
        public int LoadCount(List<Condition> conditions)
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            PrepareConditionalParameters(command, conditions);

            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_Siteuser_LoadConditionalPagedCount.ToString());

            int returnValue = (int)GetOneData(command);
            return returnValue;
        }

        /// <summary>
        /// Instance selection method that fetches data using paging and sorting.
        /// </summary>
        /// <param name="oderBy">Sorting order</param>
        /// <param name="startRow">Page start position</param>
        /// <param name="pageSize">Number of maximum record to be shown</param>
        /// <returns>List of instances of loaded objects</returns>
        public List<Siteuser> Load(string orderBy, int startRow, int pageSize)
        {
            List<Siteuser> siteusers = new List<Siteuser>();
            
			SqlCommand command = (SqlCommand)CreateCommand();
			
            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddCommandParameter(command, "@OrderBy", SqlDbType.NVarChar, 50, ParameterDirection.Input, orderBy);
            AddCommandParameter(command, "@StartRow", SqlDbType.Int, 0, ParameterDirection.Input, startRow);
            AddCommandParameter(command, "@PageSize", SqlDbType.Int, 0, ParameterDirection.Input, pageSize);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_Siteuser_LoadPaged.ToString());

            List<BaseDAO> dataList = GetListData(command);

            foreach (BaseDAO data in dataList)
            {
                Siteuser siteuser = (Siteuser)data;
                siteusers.Add(siteuser);
            }

            return siteusers;
        }

        /// <summary>
        /// Counts the number of total record available
        /// </summary>
        /// <returns>Count of the records</returns>
        public int LoadCount()
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_Siteuser_LoadPagedCount.ToString());

            int returnValue = (int)GetOneData(command);
            return returnValue;
        }

        /// <summary>
        /// Instance selection method. Used for reloading the current object from table. This method uses
        /// the Id of the current object to load and expects that the Id is correct.
        /// </summary>
        /// <returns>Boolean result as success or failure</returns>
        public override bool Load()
        {
			SqlCommand command = (SqlCommand)CreateCommand();
			
            AddCommandParameter(command, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddCommandParameter(command, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, Id);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_Siteuser_Load.ToString());

            List<BaseDAO> dataList = GetListData(command);

            foreach (BaseDAO data in dataList)
            {
                Siteuser siteuser = (Siteuser)data;

                this._id = siteuser._id;
				this._userId = siteuser._userId;
				this._userroleId = siteuser._userroleId;
				this._userPassword = siteuser._userPassword;
				this._userEmail = siteuser._userEmail;
				this._isActive = siteuser._isActive;
				this._isOnline = siteuser._isOnline;
				this._isConfirmed = siteuser._isConfirmed;
				this._confirmationCode = siteuser._confirmationCode;
				this._registerDate = siteuser._registerDate;
				this._lastLogin = siteuser._lastLogin;
				this._securityQuestion = siteuser._securityQuestion;
				this._securityAnswer = siteuser._securityAnswer;

                break;
            }

            return (dataList.Count > 0 ? true : false);
        }

        /// <summary>
        /// Instance selection method. Used for loading multiple record based on provided conditions.
        /// </summary>
        /// <param name="conditions">Custom condition based on which to match data</param>
        /// <returns>List of instances of loaded objects</returns>
        public List<Siteuser> Load(List<Condition> conditions)
        {
            List<Siteuser> siteusers = new List<Siteuser>();

			SqlCommand command = (SqlCommand)CreateCommand();
			
            PrepareConditionalParameters(command, conditions);

            SetCommandType(command, CommandType.StoredProcedure, StoreProcedureName.Sp_Siteuser_LoadConditional.ToString());

            List<BaseDAO> dataList = GetListData(command);

            foreach (BaseDAO data in dataList)
            {
                Siteuser siteuser = (Siteuser)data;
                siteusers.Add(siteuser);
            }

            return siteusers;
        }

        /// <summary>
        /// Prepares the conditional parameters for use with Create/ Save/ Load/ Delete methods
        /// that use custom condition. Prepares the user Command object for using conditions.
        /// </summary>
        /// <param name="conditions">Custom condition based on which to match data</param>
        private void PrepareConditionalParameters(SqlCommand command, List<Condition> conditions)
        {
            StringBuilder condition = new StringBuilder();

            object id = null;
			object userId = null;
			object userroleId = null;
			object userPassword = null;
			object userEmail = null;
			object isActive = null;
			object isOnline = null;
			object isConfirmed = null;
			object confirmationCode = null;
			object registerDate = null;
			object lastLogin = null;
			object securityQuestion = null;
			object securityAnswer = null;

            for (int i = 0; i < conditions.Count; i++)
            {
                string fieldName = Utility.ToHungarianNotation(conditions[i].FieldName);

                if (conditions[i].JoiningCondition == JoinOperator.FirstValue)
                {
                    condition.Append("WHERE ");
                }
                else if (conditions[i].JoiningCondition == JoinOperator.And)
                {
                    condition.Append(" AND ");
                }
                else if (conditions[i].JoiningCondition == JoinOperator.Or)
                {
                    condition.Append(" OR ");
                }

                condition.Append(conditions[i].FieldName).Append(" ").Append(Condition.MapCondition(conditions[i].ConditionalOperator)).Append("@" + fieldName);

                if ((conditions[i].ConditionalOperator == ConditionOperator.Like ||
                    conditions[i].ConditionalOperator == ConditionOperator.NotLike))
                {
                    if (string.IsNullOrEmpty(conditions[i].Value.ToString()))
                        conditions[i].Value = "%%%";
                    else
                        conditions[i].Value = "%" + conditions[i].Value.ToString() + "%";
                }

                if (fieldName == TableColumns.Id.ToString())
                    id = conditions[i].Value;
                else if (fieldName == TableColumns.UserId.ToString())
					userId = conditions[i].Value;
				else if (fieldName == TableColumns.UserroleId.ToString())
					userroleId = conditions[i].Value;
				else if (fieldName == TableColumns.UserPassword.ToString())
					userPassword = conditions[i].Value;
				else if (fieldName == TableColumns.UserEmail.ToString())
					userEmail = conditions[i].Value;
				else if (fieldName == TableColumns.IsActive.ToString())
					isActive = conditions[i].Value;
				else if (fieldName == TableColumns.IsOnline.ToString())
					isOnline = conditions[i].Value;
				else if (fieldName == TableColumns.IsConfirmed.ToString())
					isConfirmed = conditions[i].Value;
				else if (fieldName == TableColumns.ConfirmationCode.ToString())
					confirmationCode = conditions[i].Value;
				else if (fieldName == TableColumns.RegisterDate.ToString())
					registerDate = conditions[i].Value;
				else if (fieldName == TableColumns.LastLogin.ToString())
					lastLogin = conditions[i].Value;
				else if (fieldName == TableColumns.SecurityQuestion.ToString())
					securityQuestion = conditions[i].Value;
				else if (fieldName == TableColumns.SecurityAnswer.ToString())
					securityAnswer = conditions[i].Value;
            }

            AddCommandParameter(command, "@Condition", SqlDbType.NVarChar, 2000, ParameterDirection.Input, condition.ToString());
            AddCommandParameter(command, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, id);
			AddCommandParameter(command, "@UserId", SqlDbType.NVarChar, 50, ParameterDirection.Input, userId);
			AddCommandParameter(command, "@UserroleId", SqlDbType.Int, 0, ParameterDirection.Input, userroleId);
			AddCommandParameter(command, "@UserPassword", SqlDbType.NVarChar, 150, ParameterDirection.Input, userPassword);
			AddCommandParameter(command, "@UserEmail", SqlDbType.NVarChar, 50, ParameterDirection.Input, userEmail);
			AddCommandParameter(command, "@IsActive", SqlDbType.Bit, 0, ParameterDirection.Input, isActive);
			AddCommandParameter(command, "@IsOnline", SqlDbType.Bit, 0, ParameterDirection.Input, isOnline);
			AddCommandParameter(command, "@IsConfirmed", SqlDbType.Bit, 0, ParameterDirection.Input, isConfirmed);
			AddCommandParameter(command, "@ConfirmationCode", SqlDbType.NVarChar, 150, ParameterDirection.Input, confirmationCode);
			AddCommandParameter(command, "@RegisterDate", SqlDbType.DateTime, 0, ParameterDirection.Input, registerDate);
			AddCommandParameter(command, "@LastLogin", SqlDbType.DateTime, 0, ParameterDirection.Input, lastLogin);
			AddCommandParameter(command, "@SecurityQuestion", SqlDbType.NVarChar, 50, ParameterDirection.Input, securityQuestion);
			AddCommandParameter(command, "@SecurityAnswer", SqlDbType.NVarChar, 50, ParameterDirection.Input, securityAnswer);
        }

        /// <summary>
        /// Binds the fetched data from data reader to Objects
        /// </summary>
        /// <param name="reader">Data reader supplied by BaseDAO</param>
        /// <returns>List of object instances that was bound</returns>
        protected override List<BaseDAO> BindData(DbDataReader reader)
        {
            List<BaseDAO> result = new List<BaseDAO>();

			if (reader == null)
                return null;

            while (reader.Read())
            {
                Siteuser siteuser = new Siteuser
                (
					reader["id"] == System.DBNull.Value ? GetIdMinValue : (int)reader["id"],
                    reader["user_id"] == System.DBNull.Value ? string.Empty : (string)reader["user_id"],
					reader["userrole_id"] == System.DBNull.Value ? 0 : (int)reader["userrole_id"],
					reader["user_password"] == System.DBNull.Value ? string.Empty : (string)reader["user_password"],
					reader["user_email"] == System.DBNull.Value ? string.Empty : (string)reader["user_email"],
					reader["is_active"] == System.DBNull.Value ? false : (bool)reader["is_active"],
					reader["is_online"] == System.DBNull.Value ? false : (bool)reader["is_online"],
					reader["is_confirmed"] == System.DBNull.Value ? false : (bool)reader["is_confirmed"],
					reader["confirmation_code"] == System.DBNull.Value ? string.Empty : (string)reader["confirmation_code"],
					reader["register_date"] == System.DBNull.Value ? SqlDateTime.MinValue.Value : (DateTime)reader["register_date"],
					reader["last_login"] == System.DBNull.Value ? SqlDateTime.MinValue.Value : (DateTime)reader["last_login"],
					reader["security_question"] == System.DBNull.Value ? string.Empty : (string)reader["security_question"],
					reader["security_answer"] == System.DBNull.Value ? string.Empty : (string)reader["security_answer"]
                );

                result.Add(siteuser);
            }
            
            if(!reader.IsClosed)
                reader.Close();

            return result;
        }

        /// <summary>
        /// List of store procedures that this class use
        /// </summary>
        private enum StoreProcedureName
        {
            Sp_Siteuser_Save = 0,
            Sp_Siteuser_Create = 1,
            Sp_Siteuser_DeleteOne = 2,
            Sp_Siteuser_DeleteMultiple = 3,
            Sp_Siteuser_DeleteConditional = 4,
            Sp_Siteuser_SaveConditional = 5,
            Sp_Siteuser_LoadConditional = 6,
            Sp_Siteuser_Load = 7,
            Sp_Siteuser_LoadConditionalPaged = 8,
            Sp_Siteuser_LoadConditionalPagedCount = 9,
            Sp_Siteuser_LoadPaged = 10,
            Sp_Siteuser_LoadPagedCount = 11
        }

        /// <summary>
        /// List of table column names that this class use
        /// </summary>
        private enum TableColumns
        {
            Id = 0,
			UserId = 1,
			UserroleId = 2,
			UserPassword = 3,
			UserEmail = 4,
			IsActive = 5,
			IsOnline = 6,
			IsConfirmed = 7,
			ConfirmationCode = 8,
			RegisterDate = 9,
			LastLogin = 10,
			SecurityQuestion = 11,
			SecurityAnswer = 12
        }
    }
}